<?php
namespace app\setting\controller;
use think\Request;
use think\Loader;
use app\common\controller\AdminBase;

class ApiAdminTemplate extends AdminBase
{
    public function templateList()
    {
        $templatePath = ROOT_PATH . 'template' . DS;
        $publicTemplatePath = ROOT_PATH . DS  . 'public' . DS . 'template' . DS;
        if (is_dir($templatePath)) {
            $template = $templatePath;
        } else {
            if (is_dir($publicTemplatePath)) {
                $template = $publicTemplatePath;
            }
        }
        if (is_dir($template)) {
            foreach (fetch_dir($template) as $key => $dir) {
                $tempArray = explode(DS, $dir);
                $lastDir = $tempArray[count($tempArray) - 1];
                if (is_file($dir . DS . 'template.json')) {
                    $templateJson = file_get_contents($dir . DS . 'template.json');
                    $isThemeEdit = 0;
                    $isBuild = 0;
    				if (strpos($lastDir,'theme_diy') !== false) {
    					$isThemeEdit = 1;
                   	}
    				if (strpos($lastDir,'build_diy') !== false) {
    					$isBuild = 1;
                   	}
                    $templateArray[] = array('fileName' => $lastDir,'isThemeEdit' => $isThemeEdit, 'isBuild' => $isBuild, 'info' => json_decode($templateJson));
                }
            }
        }
        return jsonSuccess('',$templateArray);
    }

    public function templateSave()
    {
        $fileName = input('post.fileName');
        db('BaseSettings')->where('key','template')->update(array('val' => $fileName));
        return jsonSuccess('保存成功');
    }

}